//
//  tunBonjour.m
//  Genesis
//
//  Created by Kristoffer Andersen on 31/12/08.
//  Copyright 2008 Kristoffer Andersen. All rights reserved.
//
//  This file is part of Genesis.
//  
//  Genesis is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//  
//  Genesis is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with Genesis.  If not, see <http://www.gnu.org/licenses/>.

#import "tunBonjour.h"


@implementation tunBonjour


-(void)createService:(NSString *)serviceName type:(NSString *)serviceType port:(unsigned int)servicePort host:(NSString *)bHost
{
	NSString *domain = @"local.";
	
	//Native C implementation
	[[NSNotificationCenter defaultCenter]
postNotificationName:@"ConnectionDidPostText" object:[[NSString alloc] initWithFormat:@"\nStarting bonjour service on: %@\n",bHost]];
	DNSServiceRegister ( 
											&sdRef, //ref function
											0,  //flags
											kDNSServiceInterfaceIndexAny, //interface all 
											[serviceName cStringUsingEncoding:NSUTF8StringEncoding], /* may be NULL */
											[serviceType cStringUsingEncoding:NSUTF8StringEncoding], 
											[domain cStringUsingEncoding:NSUTF8StringEncoding], /*domain may be NULL */
											[bHost cStringUsingEncoding:NSUTF8StringEncoding], /* may be NULL */
											htons(servicePort), //concevt int to network byte order!
											0, 
											NULL, /* may be NULL */
											NULL, /* may be NULL */
											NULL /* may be NULL */
											);
	
	NSLog(@"Started bonjour service (%@) on: %@:%i\n",serviceType,bHost,servicePort);
	/*
	//-- COCOA implementation----
	bonjour = [[NSNetService alloc] initWithDomain:bHost type:serviceType name:serviceName port:servicePort];
	
	if (bonjour) {
		[bonjour setProtocolSpecificInformation:@"localhost.local: type A, class IN, addr 127.0.0.1"];
		[bonjour setDelegate:self];
		[bonjour publish];
	}*/
	
}

-(void)stopService
{
	// C impl.
	[[NSNotificationCenter defaultCenter]
	 postNotificationName:@"ConnectionDidPostText" object:@"Stopping bonjour broadcast!\n"];
	DNSServiceRefDeallocate(sdRef);
	NSLog(@"Stopping bonjour broadcast!");
	
	//Cocoa impl.
	didHappen = NO;
	[bonjour stop];
	
}


+(NSString *)recordForType:(NSString *)type
{

}
+(NSString *)typeForRecord:(NSString *)record
{
	
}


//-------Delegates---------------------

-(void)netService:(NSNetService *)sender didNotPublish:(NSDictionary *)errorDict
{
	NSLog(@"Bonjour Error delegate called!");
	int i;
	NSArray *errors = [errorDict allValues];
	for (i=0; i<[errorDict count]; i++) {
		NSLog(@"%@",[errors objectAtIndex:i]);
	}
	[bonjour release];
}

- (void)netServiceDidPublish:(NSNetService *)sender
{
	NSLog(@"Bonjour service created! On: %@",[bonjour domain]);
	didHappen = YES;
}

- (void)netServiceDidStop:(NSNetService *)sender
{
	NSLog(@"Bonjour publish stopped...");
	didHappen = YES;
}

- (void)netServiceWillPublish:(NSNetService *)sender
{
	NSLog(@"Bonjour ready to publish!");
	
}

@end
